package cn.edu.jssvc.litai.xunfeitest20210601238;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;

import java.text.BreakIterator;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText inputEt;
    private Button ttsBtn;
    private Button clearBtn;
 //   private BreakIterator input_et;

    SpeechSynthesizer tts;//语音合成对象

    //定制发音人
    String voiceSc ="x2_xiaorong"; //四川话


    //引擎类型：云端，本地
    String engineType=SpeechConstant.TYPE_CLOUD;

    void setTtsParams(){
        tts.setParameter(SpeechConstant.PARAMS,null);
        if (engineType.equals(SpeechConstant.TYPE_CLOUD)){//云端
            tts.setParameter(SpeechConstant.SPEED,"60");//语速，默认为50，取值范围0-100
            tts.setParameter(SpeechConstant.PITCH,"80");//语调，默认为50，取值范围0-100
            tts.setParameter(SpeechConstant.VOICE_NAME,voiceSc);//发音人
            tts.setParameter(SpeechConstant.SAMPLE_RATE,"16000");//采样率，采样率越高，声音越好，默认为8000或者16000
        }else {//本地离线

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        isGrantedPermission(this);
        initTts();

    }

    void initTts(){
        // 将“12345678”替换成您申请的APPID，申请地址：http://www.xfyun.cn
        // 请勿在“=”与appid之间添加任何空字符或者转义符
        SpeechUtility.createUtility(this, SpeechConstant.APPID +"=12345");
        tts=SpeechSynthesizer.createSynthesizer(this,initListener);
    }


    InitListener initListener=new InitListener() {
        @Override
        public void onInit(int i) {//初始化工作
            if (i!= ErrorCode.SUCCESS){
                Log.d("tag-ttsinit","onInit:错误码："+i);
            }
        }
    };

    private void initView() {
        inputEt = (EditText) findViewById(R.id.input_et);
        ttsBtn = (Button) findViewById(R.id.tts_btn);
        clearBtn = (Button) findViewById(R.id.clear_btn);

        ttsBtn.setOnClickListener(this);
        clearBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tts_btn:
                String msg=inputEt.getText().toString().trim();
                setTtsParams();
                if (TextUtils.isEmpty(msg)==false){
                  int result=tts.startSpeaking(msg,ttsListener);
                  if (result!=ErrorCode.SUCCESS){
                      Log.d("tag-ttsBtn", "onClick: 合成失败");
                  }
                }

                break;
            case R.id.clear_btn:

                break;
        }
    }

    SynthesizerListener ttsListener=new SynthesizerListener() {
        @Override
        public void onSpeakBegin() {
            Log.d("tag-ttsl", "onSpeakBegin: 合成开始");
        }

        @Override
        public void onBufferProgress(int i, int i1, int i2, String s) {

        }

        @Override
        public void onSpeakPaused() {

        }

        @Override
        public void onSpeakResumed() {

        }

        @Override
        public void onSpeakProgress(int i, int i1, int i2) {

        }

        @Override
        public void onCompleted(SpeechError speechError) {
            Log.d("tag-ttsl", "onCompleted: 合成结束");

        }

        @Override
        public void onEvent(int i, int i1, int i2, Bundle bundle) {

        }
    };

    public boolean isGrantedPermission(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity.checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity.checkSelfPermission(
                Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {

            activity.requestPermissions(new String[]{
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.READ_CONTACTS,
                    Manifest.permission.CAMERA
            }, 1);

            return false;
        }

        return true;
    }

//    private void submit() {
//        // validate
//        String et = input_et.getText().toString().trim();
//        if (TextUtils.isEmpty(et)) {
//            Toast.makeText(this, "输入要合成的文本", Toast.LENGTH_SHORT).show();
//            return;
//        }
//
//        // TODO validate success, do something
//
//
//    }
}